﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.Utils;

namespace Ccit.OA.Notify
{
    public partial class notify_class_tree : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _hidTabId.Value = (null != Request.QueryString["TABID"]) ? Request.QueryString["TABID"].ToString() : "";
                _hidWhereClause.Value = Ccit.OA.Utils.SysInterface.GetPageDataRangeSQL("NTF_A1", _hidTabId.Value);                
                if (_hidWhereClause.Value == "")
                {
                    _hidWhereClause.Value = "1=1";
                }
                initializePage();
            }
        }
        /// <summary>
        /// 初始化页面
        /// </summary>
        private void initializePage()
        {
            createTree();
            trvDepAddress.ExpandLevel = 1; 
        }
        private void createTree()
        {
            /* c创建根节点 */
            Microsoft.Web.UI.WebControls.TreeNode oRoot = new Microsoft.Web.UI.WebControls.TreeNode();
            ///调用创建根节点方法
            createRootNode(ref oRoot);
            ///添加第一层节点
            trvDepAddress.Nodes.Add(oRoot);
            ///添加节点信息
            createSubNodes(ref oRoot);
        }
        /// <summary>
        /// 创建根节点
        /// </summary>
        /// <param name="oRoot"></param>
        private void createRootNode(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        { 
            oRoot.Text = "通知类型"; 
            oRoot.ImageUrl = "../images/toolbar/Folder_open.gif";
            oRoot.NavigateUrl = "";
            oRoot.Target = "";
            oRoot.ID = "0_0";
        }
        /// <summary>
        /// 节点信息
        /// </summary>
        /// <param name="oRoot"></param>
        private void createSubNodes(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            NotifyBLL Bll = new NotifyBLL();
            string strAddrXML = Bll.GetNotiFyNodes(_hidWhereClause.Value);   // get one stair ast type 
            Microsoft.Web.UI.WebControls.TreeNode oTreeNode;
            XElement XeAddr = XElement.Parse(strAddrXML);
            string NotiID = "";
            string NotiNAME = "";
            var eAddrArray = from eAddr in XeAddr.Elements("ADDR") select eAddr;

            foreach (var eAddr in eAddrArray)
            {
                NotiID = eAddr.Element("NotiID").Value;
                NotiNAME = eAddr.Element("NotiNAME").Value;
                oTreeNode = createTreeNode(NotiNAME, NotiID);  
                oRoot.Nodes.Add(oTreeNode);
            }
            XeAddr = null;
            Bll = null;
        }
        /// <summary>
        /// 创建节点信息
        /// </summary>
        /// <param name="addrName">节点名称</param>
        /// <param name="addrId">节点id</param>
        /// <param name="hasChild">子节点id</param>
        /// <param name="state">状态</param>
        /// <returns></returns>
        private Microsoft.Web.UI.WebControls.TreeNode createTreeNode(string NotiNAME, string NotiID)
        {
            Microsoft.Web.UI.WebControls.TreeNode oNode = new Microsoft.Web.UI.WebControls.TreeNode();
            oNode.Text = NotiNAME;
            string sParam = "NotiID=" + NotiID;
            string encodeParam = Base64Lib.Encode(sParam);
            oNode.NavigateUrl = "notify_class_edit.aspx?" + encodeParam;
            oNode.ImageUrl = "../images/toolbar/filelogo.gif";
            oNode.Target = "iFrmEdit";
            oNode.ID = NotiID;
            oNode.Type = "root";
            return oNode;
        }
    }
}
